//
//  TrainingConsulotionViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/10.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "TrainingConsulotionViewController.h"
#import <TZImagePickerController.h>
#import "PBFlatBarButtonItems.h"

@interface TrainingConsulotionViewController ()
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic,strong) NSMutableArray *imageArray;
@end

@implementation TrainingConsulotionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     _imageArray = [[NSMutableArray alloc]init];
    self.title = @"培训总结";
    [self initView];
    UIBarButtonItem *otherButton = [PBFlatBarButtonItems Title:@"提   交" Target:self selector:@selector(submit)];
    [self.navigationItem setRightBarButtonItem:otherButton];
    [self registerFunction];
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.bottom.equalTo(self.view);
        }
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/training_conclusion" type:@"html"];
    
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self findTrainingClassInfo];
}

-(void) registerFunction {
    [self.webViewBridge registerHandler:@"chooseImage" handler:^(id data, WVJBResponseCallback responseCallback) {
       NSString* count = data[@"count"];
        
        dispatch_async(dispatch_get_main_queue(), ^(){
          TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9-[count intValue] delegate:self];

            // You can get the photos by block, the same as by delegate.
            // 你可以通过block或者代理，来得到用户选择的照片.
            [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                    [_imageArray removeAllObjects];
                    for(int i=0;i<photos.count;i++){
                        [_imageArray addObject:[ImageUtil image2DataURL: [photos objectAtIndex:i]]];

                    }
                    NSString *js = [NSString stringWithFormat:@"getImageList(%@)",[_imageArray mj_JSONString]];
                    [self.jznWebView evaluateJavaScript:js completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                        
                    }];
            }];
           [self presentViewController:imagePickerVc animated:YES completion:nil];
        });
    }];
    
    [self.webViewBridge registerHandler:@"submitInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        NSString *url = [NSString stringWithFormat:@"%@/trainingClass/conclusionTrainingClass" ,[URLUtil hostName]];
        [JZNRequestUtil requestUrl:url requestParams:data success:^(DataResult *dataResult) {
            if(dataResult.code == 0){
                [self.loadingProgressView stopAnimating];
                [[UIApplication sharedApplication].delegate.window makeToast:@"评价成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [[UIApplication sharedApplication].delegate.window makeToast:dataResult.msg];
            }
        } failure:^(NSError *error) {
            
        }];
    }];
}


-(void) submit{
    [self.loadingProgressView startAnimating];
    NSString *submit = [NSString stringWithFormat:@"submit('%@')",self.trainingClassId];
    [self.jznWebView evaluateJavaScript:submit completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        
    }];
}

-(void) findTrainingClassInfo{
    NSString *url = [NSString stringWithFormat:@"%@/trainingClass/findTrainingClassById" ,[URLUtil hostName]];
    NSDictionary *params = @{@"trainingClassId":_trainingClassId};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            
            NSString *clear = [NSString stringWithFormat:@"getTrainingClass(%@)",[dataResult.data mj_JSONString]];
            //[self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
